<template>
  <div class="manage">
    <el-dialog
      :title="operateType == 'add' ? '新增用户' : '编辑用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :inline="true"
        :form="operateForm"
        ref="userForm"
      >
      </common-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">新增</el-button>
      <common-form
        :formLabel="formLabel"
        :inline="true"
        :form="searchForm"
        ref="userForm"
      >
        <el-button type="primary" size="default" @click="getInitList(1,searchForm.keyword)"
          >搜索</el-button
        >
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getInitList"
      @userEdit="editTableUser"
      @delUser="delTableUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm.vue";
import CommonTable from "@/components/CommonTable.vue";
import { editUser, addUser, getUserList,delUser } from "@/api/data";
export default {
  name: "user",
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      operateFormLabel: [
        {
          model: "name",
          label: "姓名",
          type: "input",
        },
        {
          model: "age",
          label: "年龄",
          type: "input",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
        },
        {
          model: "birth",
          label: "出生日期",
          type: "date",
        },
        {
          model: "addr",
          label: "地址",
          type: "input",
        },
      ],
      operateForm: {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchForm: {
        keyword: "",
      },
      tableData: [],
      tableLabel: [
        {
          prop: "name",
          label: "姓名",
        },
        {
          prop: "age",
          label: "年龄",
        },
        {
          prop: "sexLabel",
          label: "性别",
        },
        {
          prop: "birth",
          label: "出生日期",
          width: 200,
        },
        {
          prop: "addr",
          label: "地址",
          width: 320,
        },
      ],
      config: {
        page: 1,
        total: 30,
      },
    };
  },
  methods: {
    confirm() {
      if (this.operateType == "edit") {
        editUser(this.operateForm).then((res) => {
          console.log(res);
          this.isShow = false;
          this.getInitList()
        });
      } else {
        addUser(this.operateForm).then((res) => {
          console.log(res);
          this.isShow = false;
          this.getInitList()
        });
      }
    },
    addUser() {
      this.isShow = true;
      this.operateType = "add";
      this.operateForm = {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      };
    },
    getInitList(page, name) {
      this.config.loading = true;
      
      name ? (this.config.page = 1) : "";
      page ? (this.config.page = page) : '';
      console.log(this.config.page,name)
      getUserList({ page: this.config.page, name: name }).then(
        res=>{
          this.tableData = res.data.list.map(item=>{
          item.sex=='0'?'女':'男'
          return item
          })
          console.log(res)
          this.config.total = res.data.count
          this.config.loading = false
        }
      );
    },
    editTableUser(row) {
      this.operateType = 'edit'
      this.isShow = true
      this.operateForm = row
    },
    delTableUser(row) {
      this.$confirm('此操作将永久删除该用户，是否继续？','提示',{
        confirmButtonText:'',
        cancelButtonText:'',
        type:'warning'
      }).then(()=>{
        const id = row.id
        console.log(row)
        delUser(id).then((res)=>{
          console.log(res)
          this.$message({
            type:'success',
            message:'删除成功'
          })
          this.getInitList()
        })
      }).catch((err)=>{
        console.log(err)
      })
    },
  },
  created () {
    this.getInitList()
  }
};
</script>

<style lang="less" scoped>
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
